package com.isunland.managesystem.ui;

import android.os.Bundle;
import com.google.gson.Gson;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.FlowTypeContent;
import com.isunland.managesystem.utils.MyStringUtil;
import java.util.ArrayList;

/**
 * 流程分类dialog
 * 开发者: HuXiaoDan
 * 日期: 2017/1/5 10:30
 */
public class FlowTypeDialogFragment extends BaseNetworkDialogFragment {
  //判断是待办，已办，请求，办结的流程分类
  protected static final String EXTRA_FROM =
      "com.isunland.palmstudysystem.ui.FlowTypeDialogFragment.EXTRA_FROM";
  private static final String EXTRA_IF_APPROVE_CODE =
      "com.isunland.palmstudysystem.ui.FlowTypeDialogFragment.EXTRA_IF_APPROVE_CODE";
  private int mFrom;
  private String mIfApproveCode;

  @Override public void onCreate(Bundle savedInstanceState) {
    mFrom = getArguments().getInt(EXTRA_FROM);
    mIfApproveCode = getArguments().getString(EXTRA_IF_APPROVE_CODE);
    super.onCreate(savedInstanceState);
  }

  @Override public ArrayList<CustomerDialog> resolveResponse(String arg0) {
    ArrayList<CustomerDialog> list = new ArrayList<>();
    FlowTypeContent[] dataList = new Gson().fromJson(arg0, FlowTypeContent[].class);
    if (dataList == null || dataList.length == 0) {
      return null;
    }
    for (FlowTypeContent aDataList : dataList) {
      list.add(new CustomerDialog(aDataList.getTypeName(), aDataList.getNodePath()));
    }
    return list;
  }

  @Override public String getUrl() {
    switch (mFrom) {
      //待办
      case TaskListFragment.MY_TODO_LIST:
        return ApiConst.URL_TODO_FLOWTYPE;
      //已办
      case TaskListFragment.MY_DONE_LIST:
        return MyStringUtil.equalsIgnoreCase("0", mIfApproveCode)
            ? ApiConst.URL_DONE_FLOWTYPE_COMPLETE : ApiConst.URL_DONE_FLOWTYPE;
      //请求
      case TaskListFragment.MY_REQUEST:
        return ApiConst.URL_REQUEST_FLOWTYPE;
      //办结
      case TaskListFragment.MY_COMPLETED:
        return ApiConst.URL_COMPLETED_FLOWTYPE;
      default:
        return "";
    }
  }

  /** 是否审批完成 */
  public static FlowTypeDialogFragment newInstance(int from, String ifApproveCode) {

    Bundle args = new Bundle();
    args.putInt(EXTRA_FROM, from);
    args.putString(EXTRA_IF_APPROVE_CODE, ifApproveCode);
    FlowTypeDialogFragment fragment = new FlowTypeDialogFragment();
    fragment.setArguments(args);
    return fragment;
  }

  public static FlowTypeDialogFragment newInstance(int from) {
    return FlowTypeDialogFragment.newInstance(from, null);
  }
}
